import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';

@Injectable({
    providedIn: 'root'
})
export class WebsocketService {
    ws: WebSocket;
    constructor() { }
    // 返回一个可观测的流，包括服务器返回的消息
    createObservableSocket(url: string): Observable<any> {
        this.ws = new WebSocket(url);
        return new Observable(
            observer => {
                this.ws.onmessage = (event) => observer.next(event.data);  // 成功，返回数据
                this.ws.onerror = (event) => observer.error(event); // 失败
                this.ws.onclose = (event) => observer.complete(); // 完成后，要结束

            })
    }
    // 向服务器端发送消息
    sendMessage(message: any) {
        const that = this
        // console.log(this.ws)
        if(this.ws){
            if (this.ws.readyState===1) {
                this.ws.send(JSON.stringify(message));
            }else{
                // console.log(this.ws.readyState)
            }
        }else{
            // socket可能还没连接成功，那么延迟一秒再发送消息
            setTimeout( ()=> {
                that.ws.send(JSON.stringify(message));
            },1000)
        }
    }
}
